﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using DBUtility;
using Model;

namespace DAL
{
    public class ToolRecord : BaseRepository<ToolRecordInfo>
    {
        #region 静态变量定义部分
        private const string PARM_ID = "@ID";
        private const string PARM_RECORDMODE = "@Record_Mode";
        private const string PARM_RECORDDATE = "@Record_Date";
        private const string PARM_WORKSHOP = "@Work_Shop";
        private const string PARM_TOOLTYPE = "@Tool_Type";
        private const string PARM_PRODUCTTYPE = "@ProductType";
        private const string PARM_PRODUCTTYPENAME = "@ProductTypeName";
        private const string PARM_TOOLCODE = "@Tool_Code";
        private const string PARM_TOOLNAME = "@Tool_Name";
        private const string PARM_TOOLTYPESPEC = "@Tool_Type_Spec";
        private const string PARM_TOOLENABLESN = "@Tool_EnableSN";
        private const string PARM_AMOUNT = "@Amount";
        private const string PARM_TAKER = "@Taker";
        private const string PARM_TAKERID = "@Taker_ID";
        private const string PARM_DISTRIBUTIONAREA = "@Distribution_Area";
        private const string PARM_TOOLSTATE = "@Tool_State";
        private const string PARM_REPAIRRECORD = "@Repair_Record";
        private const string PARM_REMARK = "@Remark";

        private const string PARM_STARTTIME = "@StartTime";
        private const string PARM_STOPTIME = "@StopTime";
        #endregion

        public override ToolRecordInfo LoadEntity<T1>(T1 Id)
        {
            SqlParameter param = GetParameter();
            param.Value = Id;
            return ExcuteSqlToGetEntity("tb_ToolRecord_GetbyID", param);
        }

        public override IList<ToolRecordInfo> LoadEntities()
        {
            return ExcuteSqlToGetEntities("tb_ToolRecord_GetAll", null);
        }

        public IList<ToolRecordInfo> ToolRecord_GetbyMultiFact(ToolRecordInfo tci,DateTime starttime,DateTime stoptime)
        {
            SqlParameter[] parms = GetMultiFactParameters();
            SetMultiFactParameters(tci,starttime,stoptime, parms);
            return ExcuteSqlToGetEntities("tb_ToolRecord_GetbyMultiFact", parms);
        }

        public ToolRecordInfo GetbytEsnExis(string tEsn)//, string WorkShop
        {
            SqlParameter[] parms = GetbytEsnExisParameters();
            GetbytEsnExisParameters(tEsn, parms);// WorkShop,
            return ExcuteSqlToGetEntity("tb_ToolRecord_GetbytEsnExis", parms);
        }

        public ToolRecordInfo GetbytEsnExisTcode(string tEsn,string tCode)
        {
            SqlParameter[] parms = GetTParameterseTcode();
            GetbyTcodeParameteresTcode(tEsn, tCode, parms);
            return ExcuteSqlToGetEntity("tb_ToolRecord_GetbytEsnExisTcode", parms);
        }


        public ToolRecordInfo GetbyTypeandName(string ToolCode, string Work_Shop)
        {
            SqlParameter[] parms = GetTParameterse();
            GetbyTcodeParameteres(ToolCode, Work_Shop, parms);
            return ExcuteSqlToGetEntity("tb_ToolRecord_GetbyTypeandName", parms);
        }






        private static SqlParameter[] GetTParameterseTcode()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
                    new SqlParameter(PARM_TOOLENABLESN, SqlDbType.NVarChar,50), 
					new SqlParameter(PARM_TOOLCODE, SqlDbType.NVarChar,50)
                };
            return parms;
        }

        public void GetbyTcodeParameteresTcode(string tEsn, string Tool_Code, params SqlParameter[] Parms)//
        {
            Parms[0].Value = tEsn;
            Parms[1].Value = Tool_Code;
         
        }




        private static SqlParameter[] GetTParameterse()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
					new SqlParameter(PARM_TOOLCODE, SqlDbType.NVarChar,50)
                   ,new SqlParameter(PARM_WORKSHOP, SqlDbType.NVarChar,20)
                };
            return parms;
        }

        public void GetbyTcodeParameteres(string Tool_Code, string Work_Shop, params SqlParameter[] Parms)//
        {
            Parms[0].Value = Tool_Code;
            Parms[1].Value = Work_Shop;
        }

        private void GetbytEsnExisParameters(string tEsn, SqlParameter[] parms)//string WorkShop,
        {
            parms[0].Value = tEsn;
            //Parms[1].Value = WorkShop;
        }

        private SqlParameter[] GetbytEsnExisParameters()
        {
            SqlParameter[] parms = new SqlParameter[]
                {
                    
                    //new SqlParameter(PARM_WORKSHOP , SqlDbType.NVarChar,20),
                    new SqlParameter(PARM_TOOLENABLESN , SqlDbType.NVarChar,50)
               
                };
            return parms;
        }



        //指定Command对象的类型，有3种选择：

        // Text：表示Command对象用于执行SQL语句

        // StoredProcedure：表示Command对象用于执行存储过程

        // TableDirect：表示Command对象用于直接处理某个表

        //CommandType属性的默认值为Text
        public IList<ToolRecordInfo> ToolRecord_StatisticsbyMultiFact(ToolRecordInfo tci)
        {
            SqlParameter[] Parms = GetParameters();
            SetParameters(tci, Parms);
            IList<ToolRecordInfo> Entities = new List<ToolRecordInfo>();
            using (SqlDataReader rdr = sqlHelper.ExecuteReader(sqlHelper.ConnectionStringLocalTransaction, 
                CommandType.StoredProcedure, "tb_ToolRecord_StatisticsbyMultiFactTest", Parms))//tb_ToolRecord_StatisticsbyMultiFact
            {
                while (rdr.Read())
                {
                    ToolRecordInfo Entity = new ToolRecordInfo
                    {
                        Work_Shop = rdr.GetValue(0).ToString(),
                        Tool_Type = rdr.GetValue(1).ToString(),
                        //Record_Mode = rdr.GetValue(2).ToString(),
                        ProductType = rdr.GetValue(2).ToString(),
                        ProductTypeName = rdr.GetValue(3).ToString(),
                        Tool_Code = rdr.GetValue(4).ToString(),
                        Tool_Name = rdr.GetValue(5).ToString(), 
                        Tool_Type_Spec = rdr.GetValue(6).ToString(),
                        Tool_EnableSN = rdr.GetValue(7).ToString(),
                        Amount = rdr.GetInt32(8), 
                        Distribution_Area = rdr.GetValue(9).ToString(), 
                        Tool_State = rdr.GetValue(10).ToString(),
                        Repair_Record = rdr.GetValue(11).ToString(), 
                        Remark = rdr.GetValue(12).ToString(),
                        Taker = rdr.GetValue(13).ToString(),
                        Taker_ID = rdr.GetValue(14).ToString()
                    };
                    Entities.Add(Entity);
                }
            }
            return Entities;
        }


        #region 重写基类部分
        public override string GetInsertsp()
        {
            return "tb_ToolRecord_Insert";
        }

        public override string GetUpdatesp()
        {
            return "tb_ToolRecord_Update";
        }

        public override string GetDeletesp()
        {
            return "tb_ToolRecord_Delete";
        }

        public override ToolRecordInfo GetEntityRecord(SqlDataReader rdr)
        {
            ToolRecordInfo cinfo = new ToolRecordInfo
            {
                ID = rdr.GetInt32(0),
                Record_Mode = rdr.GetValue(1).ToString(),
                Record_Date = rdr.GetDateTime(2),
                Work_Shop = rdr.GetValue(3).ToString(),
                Tool_Type = rdr.GetValue(4).ToString(),
                ProductType = rdr.GetValue(5).ToString(),
                ProductTypeName = rdr.GetValue(6).ToString(),
                Tool_Code = rdr.GetValue(7).ToString(),
                Tool_Name = rdr.GetValue(8).ToString(),
                Tool_Type_Spec = rdr.GetValue(9).ToString(),
                Tool_EnableSN = rdr.GetValue(10).ToString(),
                Amount = rdr.GetInt32(11),
                Taker = rdr.GetValue(12).ToString(),
                Taker_ID = rdr.GetValue(13).ToString(),
                Distribution_Area = rdr.GetValue(14).ToString(),
                Tool_State = rdr.GetValue(15).ToString(),
                Repair_Record = rdr.GetValue(16).ToString(),
                Remark = rdr.GetValue(17).ToString(),
            };
            return cinfo;
        }

        public override ToolRecordInfo GetEntityRecord()
        {
            ToolRecordInfo cinfo = new ToolRecordInfo();
            return cinfo;
        }

        public override SqlParameter GetParameter()
        {
            SqlParameter param = new SqlParameter(PARM_ID, SqlDbType.Int, 4);
            return param;
        }

        public override SqlParameter[] GetParameters()
        {
            SqlParameter[] parms = new SqlParameter[]
                {
                    new SqlParameter(PARM_ID, SqlDbType.Int, 4),
                    new SqlParameter(PARM_RECORDMODE , SqlDbType.NVarChar,10),
                    new SqlParameter(PARM_RECORDDATE , SqlDbType.DateTime,8),
                    new SqlParameter(PARM_WORKSHOP , SqlDbType.NVarChar,20),
                    new SqlParameter(PARM_TOOLTYPE , SqlDbType.NVarChar,20),
                    new SqlParameter(PARM_PRODUCTTYPE , SqlDbType.NVarChar,100),
                    new SqlParameter(PARM_PRODUCTTYPENAME , SqlDbType.NVarChar,200),
                    new SqlParameter(PARM_TOOLCODE , SqlDbType.NVarChar,50),
                    new SqlParameter(PARM_TOOLNAME , SqlDbType.NVarChar,50),
                    new SqlParameter(PARM_TOOLTYPESPEC   , SqlDbType.NVarChar,500),
                    new SqlParameter(PARM_TOOLENABLESN  , SqlDbType.NVarChar,50),
                    new SqlParameter(PARM_AMOUNT , SqlDbType.Int, 4),
                    new SqlParameter(PARM_TAKER , SqlDbType.NVarChar,10),
                    new SqlParameter(PARM_TAKERID , SqlDbType.NVarChar,10),
                    new SqlParameter(PARM_DISTRIBUTIONAREA , SqlDbType.NVarChar,20),
                    new SqlParameter(PARM_TOOLSTATE , SqlDbType.NVarChar,20),
                    new SqlParameter(PARM_REPAIRRECORD , SqlDbType.NVarChar,20),
                    new SqlParameter(PARM_REMARK  , SqlDbType.NVarChar,200),
                };
            return parms;
        }

        public override void SetParameters(ToolRecordInfo Entity, params SqlParameter[] Parms)
        {
            Parms[0].Value = Entity.ID;
            Parms[1].Value = Entity.Record_Mode;
            Parms[2].Value = Entity.Record_Date;
            Parms[3].Value = Entity.Work_Shop;
            Parms[4].Value = Entity.Tool_Type;
            Parms[5].Value = Entity.ProductType;
            Parms[6].Value = Entity.ProductTypeName;
            Parms[7].Value = Entity.Tool_Code;
            Parms[8].Value = Entity.Tool_Name;
            Parms[9].Value = Entity.Tool_Type_Spec;
            Parms[10].Value = Entity.Tool_EnableSN;
            Parms[11].Value = Entity.Amount;
            Parms[12].Value = Entity.Taker;
            Parms[13].Value = Entity.Taker_ID;
            Parms[14].Value = Entity.Distribution_Area;
            Parms[15].Value = Entity.Tool_State;
            Parms[16].Value = Entity.Repair_Record;
            Parms[17].Value = Entity.Remark;
        }

        #endregion


        public SqlParameter[] GetMultiFactParameters()
        {
            SqlParameter[] parms = new SqlParameter[]
                {
                    new SqlParameter(PARM_RECORDMODE , SqlDbType.NVarChar,10),
                    new SqlParameter(PARM_WORKSHOP , SqlDbType.NVarChar,20),
                    new SqlParameter(PARM_TOOLTYPE , SqlDbType.NVarChar,20),
                    new SqlParameter(PARM_DISTRIBUTIONAREA , SqlDbType.NVarChar,20),
                    new SqlParameter(PARM_TOOLCODE , SqlDbType.NVarChar,50),
                    new SqlParameter(PARM_TOOLNAME , SqlDbType.NVarChar,50),                    
                    new SqlParameter(PARM_STARTTIME  , SqlDbType.Date),
                    new SqlParameter(PARM_STOPTIME  , SqlDbType.Date),
                };
            return parms;
        }

        public void SetMultiFactParameters(ToolRecordInfo Entity,DateTime starttime,DateTime stoptime, params SqlParameter[] Parms)
        {
            Parms[0].Value = Entity.Record_Mode;
            Parms[1].Value = Entity.Work_Shop;
            Parms[2].Value = Entity.Tool_Type;
            Parms[3].Value = Entity.Distribution_Area;
            Parms[4].Value = Entity.Tool_Code;
            Parms[5].Value = Entity.Tool_Name;
            Parms[6].Value = starttime;
            Parms[7].Value = stoptime;
        }

        
    }
}
